package com.rulin.asp.business.controller;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rulin.asp.business.entity.Question;
import com.rulin.asp.business.entity.QuestionParam;
import com.rulin.asp.business.entity.QuestionResultRow;
import com.rulin.asp.business.log.entity.QuestionLog;
import com.rulin.asp.business.log.service.QuestionLogService;
import com.rulin.asp.business.service.DictionaryService;
import com.rulin.asp.business.service.QuestionService;
import com.rulin.asp.sso.entity.UserUtils;
import com.rulin.asp.util.Consists;
import com.rulin.asp.util.Tools;

@Controller
@RequestMapping("/quesList")
public class QuesListController {

	@Autowired
	private QuestionService questionService;
	
	@Autowired
	private DictionaryService dictionaryService;
	
	@Autowired
	private QuestionLogService questionLogService;
	
	@RequestMapping(value = "/quesListPage.html", method = RequestMethod.GET)
	public String quesListPage(Model model){
		setDiv(model);
		return "business/queslist";
	}
	
	public void setDiv(Model model){
		model.addAttribute("quesTypes", dictionaryService.getByModel("ques_type_param"));
		model.addAttribute("quesDiffLevels", dictionaryService.getByModel("ques_diff_level"));
		
		model.addAttribute("questionSources", dictionaryService.getByModel("question_source"));
		model.addAttribute("quesBookClasses", dictionaryService.getByModel("ques_book_class"));
		model.addAttribute("quesBookClasses1", dictionaryService.getByModel("ques_book_class_1"));
		model.addAttribute("quesBookClasses2", dictionaryService.getByModel("ques_book_class_2"));
		model.addAttribute("quesBookClasses3", dictionaryService.getByModel("ques_book_class_3"));
		model.addAttribute("userTypes", dictionaryService.getByModel("user_type"));
		model.addAttribute("quesBookClasses4", dictionaryService.getByModel("ques_book_class_4"));
	}
	
	@RequestMapping(value = "/getList.json", method = RequestMethod.GET)
	@ResponseBody
	public String list(QuestionParam bean){
		Pair<Integer,List<QuestionResultRow>> pair= questionService.getQuesListByParam(bean);
		return Tools.toEasyuiDataGrid(pair.getLeft(), pair.getRight());
	}
	
	/**
	 * 进入认领题目页面
	 * @param model
	 * @param quesId
	 * @return
	 */
	@RequestMapping(value = "/openQuesTeacherPage.html", method = RequestMethod.GET)
	public String openQuesTeacherPage(Model model,String quesId){
		setDiv(model);
		
		Question question = questionService.getQuestion(quesId);
		model.addAttribute("form", question);
		
		String userName = UserUtils.getUser().getUserName();
		String userAccount = UserUtils.getUser().getAccount();
		question.setClaimUserAccount(userAccount);
		question.setClaimUserName(userName);
		question.setQuesType(Consists.QUES_TYPE_2);
		questionService.saveQuestion(question);
		
		//保存日志信息
		QuestionLog log = new QuestionLog();
		log.setQuesId(question.getQuesId());
		log.setOperateType("4");
		log.setUserAccount(UserUtils.getUser().getAccount());
		log.setUserName(UserUtils.getUser().getUserName());
		questionLogService.addQuestionLog(log);
		
		return "business/questeacher";
	}
	
	@RequestMapping(value = "/back.json", method = RequestMethod.POST)
	public void back(Question question){
		question.setQuesType(Consists.QUES_TYPE_1);
		questionService.saveQuestion(question);
		
		//保存日志信息
		QuestionLog log = new QuestionLog();
		log.setQuesId(question.getQuesId());
		log.setOperateType("6");
		log.setUserAccount(UserUtils.getUser().getAccount());
		log.setUserName(UserUtils.getUser().getUserName());
		questionLogService.addQuestionLog(log);
	}
	
	@RequestMapping(value = "/claim.json", method = RequestMethod.POST)
	public void claim(Question question){
		String userName = UserUtils.getUser().getUserName();
		String userAccount = UserUtils.getUser().getAccount();
		question.setClaimUserAccount(userAccount);
		question.setClaimUserName(userName);
		question.setClaimTime(new Date());
		question.setQuesType(Consists.QUES_TYPE_3);
		questionService.saveQuestion(question);
		
		//保存日志信息
		QuestionLog log = new QuestionLog();
		log.setQuesId(question.getQuesId());
		log.setOperateType("5");
		log.setUserAccount(UserUtils.getUser().getAccount());
		log.setUserName(UserUtils.getUser().getUserName());
		questionLogService.addQuestionLog(log);
	}
	
	@RequestMapping(value = "/isClaimed.json", method = RequestMethod.POST)
	@ResponseBody
	public String isClaimed(Question param){
		Question question = questionService.getQuestion(param.getQuesId());
		return question.getQuesType();
	}
	
	@RequestMapping(value = "/openQuesReadPage.html", method = RequestMethod.GET)
	public String openQuesReadPage(Model model,String quesId){
		setDiv(model);
		Question question = questionService.getQuestion(quesId);
		model.addAttribute("form", question);
		return "business/quesread";
	}
	
	/**
	 * 将待审核的图书纠错入库问题池中
	 * @param quesId
	 */
	@RequestMapping(value = "/addPool.json", method = RequestMethod.POST)
	public void addPool(String quesId){
		Question question = new Question();
		question.setQuesId(quesId);
		question.setQuesType(Consists.QUES_TYPE_1);
		questionService.saveQuestion(question);
		//保存题目日志信息
		QuestionLog log = new QuestionLog();
		log.setQuesId(question.getQuesId());
		log.setOperateType("1");
		log.setUserAccount(UserUtils.getUser().getAccount());
		log.setUserName(UserUtils.getUser().getUserName());
		questionLogService.addQuestionLog(log);
	}
}
